(async function(){
    if (!checkConnect()) return;
    setLoading(1);
    var groups = await getMyGroup();
    var rtn = await loadManbers();
    setLoading(0);
    if (rtn){
        var my = getMy();
        if (!my || !my.isReal){
            alert("您还不是正式会员");
            return;
        }
        //设置小组列表
        for(var i=0;i<groups.length;i++){
            var option = newNode("option");
            option.innerText = groups[i].name;
            option.value = groups[i].id;
            getNode("groups").appendChild(option);
        }
        var ts = new Date().getTime() + 3*24*3600*1000;
        getNode("endTime").value = new Date(ts).format("yyyy/MM/dd");

        //添加选项按钮
        getNode("addOption").onclick=function(){
            var list = getNode("options");
            var line = list.children[0];
            var newLine = line.cloneNode(true);
            list.appendChild(newLine);
            newLine.children[0].value="";
        };
        //删除选项按钮
        getNode("removeOption").onclick=function(){
            var list = getNode("options");
            if (list.children.length>1){
                removeNode(list.children[list.children.length-1]);
            }
        };
        //添加确定点击
        getNode("addVote").onclick=async function(){
            if (!checkConnect()) return;
            //获取输入值
            var name = getNode("name").value;
            var group = getNode("groups").options[getNode("groups").selectedIndex].value;
            var endTime = getNode("endTime").value;
            var voteText = getNode("voteText").value;
            var list = getNode("options");
            var options = [];
            for(var i=0;i<list.children.length;i++){
                if (list.children[i].children[0].value!="") options.push(list.children[i].children[0].value);
            }
            //验证输入值
            if (name.length==0) {
                alert("投票标题不能为空");
                return;
            }
            if (voteText.length==0) {
                alert("投票内容不能为空");
                return;
            }
            if (options.length==0) {
                alert("选项不能为空");
                return;
            }
            if (endTime.length!=10 || endTime.substr(4,1)!="/" || endTime.substr(7,1)!="/") {
                alert("截止时间不正确，请按照yyyy/mm/dd格式输入");
                return;
            }
            try{
                endTime = getUNIXTimestamp(endTime);
            }catch(e){
                alert("截止时间不正确，请按照yyyy/mm/dd格式输入");
                return;
            }

            setLoading(1);
            const rtn = await newVote("创建投票",name,voteText,options,endTime,group);
            if (rtn){
                //载入小组列表
                publicParam.groups = null;
                goto("vote/list");
            }
            setLoading(0);
        };
    }
})();